package com.aiju.cash.ui.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.bean.Shop;
import com.aiju.cash.R;

import java.util.List;

/**
 * 作者：流冥 on 2017/6/15 16:52
 * 说明：
 */

public class SelectShopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Shop> mList;

    public SelectShopAdapter(Context context, List<Shop> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_select_shop, parent, false);

        return new ContentViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        showView((ContentViewHolder) holder, mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    private class ContentViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        ImageView check;
        TextView title;
        ImageView top;

        public ContentViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            check = (ImageView) itemView.findViewById(R.id.check);
            title = (TextView) itemView.findViewById(R.id.title);
            top = (ImageView) itemView.findViewById(R.id.top);
        }
    }

    void showView(ContentViewHolder holder, final Shop selectShop) {
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clear();
                selectShop.setSelected(true);
                mListener.setOnItemClickListenering(selectShop.getStoreId());
                notifyDataSetChanged();
            }
        });
        holder.title.setText(selectShop.getShopName());
        String isReputationShop = selectShop.getIsReputationShop();
        if ("1".equals(isReputationShop)) {//口碑门店有角标
            holder.top.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(selectShop.getReviewStatus()) && selectShop.getReviewStatus().equals("1")) {
                holder.top.setImageResource(R.mipmap.icon_left1);
            } else {
                holder.top.setImageResource(R.mipmap.icon_left2);
            }
        }else{//普通门店无角标
            holder.top.setVisibility(View.GONE);
        }
        showTextAndCheckImageView(holder, selectShop.isSelected());
    }

    void showTextAndCheckImageView(ContentViewHolder holder, boolean isSelect) {
        if (isSelect) {
            holder.title.setTextColor(mContext.getResources().getColor(R.color.color_00A0E8));
            holder.check.setVisibility(View.VISIBLE);
        } else {
            holder.title.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            holder.check.setVisibility(View.GONE);
        }

    }

    /**
     * 筛选出被选择的店铺，若没有被选择的，返回null
     */
    public Shop getSelectedShop() {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).isSelected()) {
                return mList.get(i);
            }
        }
        return null;
    }

    public void clear() {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setSelected(false);
        }
    }

    public void setList(List<Shop> list) {
        mList = list;
        notifyDataSetChanged();

    }

    public interface  OnItemClickListener{
        void setOnItemClickListenering(String storeId);
    }

    private OnItemClickListener mListener;
    public void setOnItenClickListner(OnItemClickListener listner){
        mListener = listner;
    }

}
